package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.Board;
import com.example.forum.model.User;
import com.example.forum.services.IArticleService;
import com.example.forum.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wenjingyuan
 * @Date: 2023/09/25/11:02
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/article")
@Api(tags = "文章接口")
public class ArticleController {

    @Resource
    private IBoardService boardService;
    @Resource
    private IArticleService articleService;
    /**
     * 发布新帖
     * @param boardId
     * @param title
     * @param content
     * @return
     */
    @ApiOperation("发布新帖")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("板块ID") @RequestParam("boardId") @NonNull Long boardId,
                            @ApiParam("文章标题") @RequestParam("title") @NonNull String title,
                            @ApiParam("文章内容") @RequestParam("content") @NonNull String content){

        //查看用户是否处于禁言状态
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            //用户已经禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        //板块的校验
        Board board = boardService.selectById(boardId);
        if(board==null || board.getState()==1 || board.getDeleteState()==1){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            //返回错误信息
            return AppResult.failed(ResultCode.FAILED_BOARD_BANNED);
        }

        //封装文章对象
        Article article=new Article();
        article.setContent(content);
        article.setTitle(title);
        article.setBoardId(boardId);
        article.setUserId(user.getId());
        //调用service接口
        articleService.Create(article);
        return AppResult.success();
    }

    /**
     * 获取帖子的列表
     * @param boardId
     * @return
     */
    @ApiOperation("获取帖子的列表")
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId(@ApiParam("板块Id") @RequestParam(value = "boardId",required = false) Long boardId){

        //定义一个变量
        List<Article> articles;
        if(boardId==null){
          //查询所有的
           articles = articleService.selectAll();

      }else{
          articles=articleService.selectAllByBoardId(boardId);
      }
        if(articles==null){
            //如果为空，则创建一个空集合
            articles=new ArrayList<>();
        }
        //相应结果
        return AppResult.success(articles);
    }

    @ApiOperation("根据帖子id获取帖子详情")
    @GetMapping("/details")
    public AppResult<Article> getDetails(
            HttpServletRequest request,
            @ApiParam("帖子ID") @RequestParam("id") @NonNull Long id){
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用service
        Article article = articleService.selectDetailById(id);
        //判断article是否为空
        if(article==null){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(user.getId()==article.getUserId()){
            //是同一个人
            article.setOwn(true);
        }
        return AppResult.success(article);
    }
    @ApiOperation("编辑帖子")
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,
                            @ApiParam("帖子Id") @RequestParam("id") @NonNull Long id,
                            @ApiParam("帖子标题") @RequestParam("title") @NonNull String title,
                            @ApiParam("帖子正文") @RequestParam("content") @NonNull String content){
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        //校验用户的状态
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        Article article = articleService.selectById(id);
        //校验帖子是否有效
        if(article==null){
            //返回错误信息
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断你是不是作者
        if(article.getUserId()!=user.getId()){
            //返回错误信息
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //判断帖子状态是否是结束状态
        if(article.getState()==1 ||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }

        //调用SERVICE
        articleService.modify(id,title,content);
        //打印日志
        log.info("修改成功 article.id"+article.getId()+"user id"+user.getId()+".");
        return AppResult.success();
    }

    @ApiOperation("点赞")
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request,@ApiParam("帖子id") @RequestParam("id") @NonNull Long id){
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        //校验用户的状态
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //调用service
        articleService.thumbsUpById(id);
        //返回结果
        return AppResult.success();
    }
    @ApiOperation("删除帖子")
    @PostMapping("/delete")
    public AppResult deleteById(HttpServletRequest request,
                                @ApiParam("帖子ID") @RequestParam("id") @NonNull Long id){
        //拿到用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //调用service
        Article article = articleService.selectById(id);
        if(article==null){
            //返回错误信息
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断你是不是作者
        if(article.getUserId()!=user.getId()){
            //返回错误信息
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //判断帖子状态是否是结束状态
        if(article.getState()==1 ||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //调用service
        articleService.deleteById(id);
        log.info("删除成功 article id:"+article.getId());
        return AppResult.success();
    }

    @ApiOperation("根据用户id获取帖子列表")
    @GetMapping("/getAllByUserId")
    public AppResult<List<Article>> getAllByUserId(HttpServletRequest request,
            @ApiParam("用户ID") @RequestParam(value = "userId",required = false) Long userId){
        //如果用户id为空，那么从session中获取
        if(userId==null){
            //拿到用户
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute(AppConfig.USER_SESSION);
            userId=user.getId();
        }
        List<Article> articles = articleService.selectByUserId(userId);
        return AppResult.success(articles);
    }
}
